%Run data_generate.m first.
%What this does: Calculating training loss surface and plotting it.

shouldwePlot=1;
last2paramsPlot=1; % a flag to turn on one of the plots.


%% **Works only for 1X1 B1 array (default)** Plot Original training data
%% for visual inspection in feature space
if (shouldwePlot==1 && last2paramsPlot==1 && length(B1)==1)
    figure;
    pos = find(trainingdata(:,3)==1);
    plot(trainingdata(pos,1),trainingdata(pos,2),'b.');
    pos = find(trainingdata(:,3)==-1);
    hold on; plot(trainingdata(pos, 1),trainingdata(pos, 2),'r.');     
    axis([-3 3 -3 3]);
    x = [-1:0.1:1];
    epsilonval = 1e-6;
    for j=1:length(B2)
        for k=1:length(B3)
            m = -(B2(k)/(B3(j)+epsilonval));
            c = -B1(end)/(B3(j)+epsilonval);    %caution.
            y = m*x +c;
            plot(x,y);
        end
    end            
    hold off;
end

%% Plotting the traing loss surface over the 2D parameter grid
if(shouldwePlot==1)
    figure;
end
for i=1:length(B1)
    for j=1:length(B2)
        for k=1:length(B3)
            
        %trainingloss computation for each of these values 
        FtrainExhausive=B1(i)+trainingdata(:,1)*B2(j) + trainingdata(:,2)*B3(k);
        lossTraining(i,j,k) = sum(log(1+exp(-trainingdata(:,3).*FtrainExhausive)));
        end
    end
    if(last2paramsPlot==1)
        if(length(B1)==1) %default
            surf(squeeze(lossTraining(i,:,:)));
        else
            hold on;
            subplot(2,length(B1)/2,i); surf(B2,B3,squeeze(lossTraining(i,:,:)));axis tight; %axis([-1 2.8 -1 2.8])
        end
    end
    min(min(squeeze(lossTraining(i,:,:))))
    hold off;
end

%Note: Loss should be convex wrt 2 params given y,x. Similarly, given
%params, x it is convex wrt y. The function f(x) is linear.
